/**
 * @param {fn: func}
 * @param {delay: time}
 * @param {immediate: boolean}
 */

export function debounce(fn, delay = 1000, immediate = true) {
	let timer
	return function () {
		let _self = this
			let args = arguments

		if (timer) clearTimeout(timer)
		if (immediate) {
			let callNow = !timer
			timer = setTimeout(() => {
				timer = null
			}, delay)
			if (callNow) fn.apply(_self, args)
		} else {
			timer = setTimeout(() => {
				fn.apply
			}, delay)
		}
	}
}